package com.point.community;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.demo.SysRoleFuncInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysRoleFunc;
import com.point.community.bean.dwzPageData;

@SuppressWarnings("serial")
public class SysRoleFuncAction extends JsonAction {

	Logger logger = Logger.getLogger(SysRoleFuncAction.class);
	@Autowired
	private SysRoleFuncInterface sysRoleFuncService;

	private String msg = "";
	private boolean success = false;
	private String key = "";
	private String obj = "";
	private PageBean pageBean;
	private String statusCode,message,navTabId,rel,callbackType,forwardUrl,confirmMsg;
	
	private List<SysRoleFunc> sysRoleFuncList = new ArrayList<SysRoleFunc>();

	public SysRoleFuncInterface getSysRoleFuncService() {
		return sysRoleFuncService;
	}

	public void setSysRoleFuncService(SysRoleFuncInterface sysRoleFuncService) {
		this.sysRoleFuncService = sysRoleFuncService;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getObj() {
		return obj;
	}

	public void setObj(String obj) {
		this.obj = obj;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNavTabId() {
		return navTabId;
	}

	public void setNavTabId(String navTabId) {
		this.navTabId = navTabId;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getCallbackType() {
		return callbackType;
	}

	public void setCallbackType(String callbackType) {
		this.callbackType = callbackType;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public String getConfirmMsg() {
		return confirmMsg;
	}

	public void setConfirmMsg(String confirmMsg) {
		this.confirmMsg = confirmMsg;
	}

	public List<SysRoleFunc> getSysRoleFuncList() {
		return sysRoleFuncList;
	}

	public void setSysRoleFuncList(List<SysRoleFunc> sysRoleFuncList) {
		this.sysRoleFuncList = sysRoleFuncList;
	}
	
	@SuppressWarnings("unchecked")
	String webQueryAllSysRoleFunc()
	{
		String currentPage = request.getParameter("pageNum");
		String numPerPage = request.getParameter("numPerPage");
		if(StringUtils.isEmpty(currentPage)){
			currentPage = "1";
	    }
	    if(StringUtils.isEmpty(numPerPage)){
	    	numPerPage = "20";
	    }
	    int nCurrentPage =  Integer.parseInt(currentPage);    
	    int nNumPerPage =  Integer.parseInt(numPerPage);  
		
		String start = getParameterString("start");
		if (start != null && !start.equals("")) {
			pageBean = new PageBean(start, PAGE_MAX_RECORD_COUNT);
		} else {
			pageBean = new PageBean("0", PAGE_MAX_RECORD_COUNT);
		}
		//参数
		String rolecode = "", func = "";
		if(request.getParameterValues("rolecode")!=null)
			rolecode = getParameterString("rolecode");
		if(request.getParameterValues("func")!=null)
			func = getParameterString("func");		 
		
		pageBean = sysRoleFuncService.queryAllSysRoleFunc(pageBean, rolecode, func);
		
		sysRoleFuncList = pageBean.getResultList();

		request.getSession().setAttribute("sysRoleFuncList", sysRoleFuncList);

		dwzPageData dwz = new dwzPageData();
		dwz.currentPage = Integer.valueOf(nCurrentPage).toString();
		dwz.numPerPage =  Integer.valueOf(nNumPerPage).toString();
		dwz.totalCount = (long) pageBean.getTotal();
		request.getSession().setAttribute("sysRoleFuncDwzData", dwz);
		
		return "sysRoleFunc";
	}
	
	public String jsonQueryAllSysRoleFunc()
	{
		logger.error("jsonQueryAllSysRoleFunc");
		//参数
		int roleid = -1;
		if (request.getParameterValues("roleid") != null)
			roleid = getParameterInteger("roleid");
		String rolecode = "";
		if (request.getParameterValues("rolecode") != null)
			rolecode = getParameterString("rolecode");
		if(roleid!=-1)
			sysRoleFuncList = sysRoleFuncService.queryAllSysRoleFuncList(roleid);
		else
			sysRoleFuncList = sysRoleFuncService.queryAllSysRoleFuncList(rolecode);
		
		boolean ret = sysRoleFuncList==null?false:sysRoleFuncList.size()>0;
		
		if (ret) 
		{
			success = true;
			msg = "请求成功！";
		} else {
			success = false;
			msg = "请求失败或无数据！";
		}
		return SUCCESS;
	}
	
	public String jsonUpdateSysRoleFunc()
	{
		int roleid = 0;
		String funclist = "";
		if (request.getParameterValues("roleid") != null)
			roleid = getParameterInteger("roleid");
		
		if (request.getParameterValues("funclist") != null)
			funclist = getParameterString("funclist");
		
		boolean ret = sysRoleFuncService.updateSysRoleFuncs(roleid, funclist);
		
		if (ret) 
		{
			success = true;
			msg = "请求成功！";
		} else {
			success = false;
			msg = "请求失败或无数据！";
		}
		return SUCCESS;
	}

	public static void main(String args[]) {
		int roleid = 0;
		String funclist = "4:1;5:0";
		List<Integer> funcs = new ArrayList<Integer>();
		List<Integer> rights = new ArrayList<Integer>();
		String sSql = "";
		String[] funcright = funclist.split(";");
		for (int i = 0; i < funcright.length; i++) {
			String item = funcright[i];
			System.out.println("item=" + item);
			String subItems[] = item.split(":");
			if (subItems.length >= 2) {
				funcs.add(Integer.valueOf(subItems[0]));
				rights.add(Integer.valueOf(subItems[1]));
			}
		}
		if (funcs.size() < 1) {
			sSql = "delete from sys_rolefunc where roleid=" + Integer.toString(roleid);
			System.out.println(sSql);
		}
		String where="";
		for (int i = 0; i < funcs.size(); i++) {
			if(StringUtils.isEmpty(where))
				where = funcs.get(i).toString();
			else
				where += ","+funcs.get(i).toString();
		}
		sSql = "delete from sys_rolefunc where roleid=" + Integer.toString(roleid)+" and funcid not in ("+where+")";
		System.out.println(sSql);
	}
}
